﻿using System;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 单位转换器
    /// </summary>
    public class UnitConverter
    {

        #region 频率转换

        /// <summary>
        /// 频率Hz转换为KHz
        /// </summary>
        /// <param name="freq">单位:Hz</param>
        /// <returns>单位:KHz</returns>
        public static double FreqHzToKHz(long freq)
        {
            return (double)freq / 1000;
        }

        /// <summary>
        /// 频率Hz转换为MHz
        /// </summary>
        /// <param name="freq">单位:hz</param>
        /// <returns>单位:MHz</returns>
        public static double FreqHzToMHz(long freq)
        {
            return (double)freq / 1000000;
        }

        /// <summary>
        /// 频率Hz转换为GHz
        /// </summary>
        /// <param name="freq">单位:hz</param>
        /// <returns>单位:GHz</returns>
        public static double FreqHzToGHz(long freq)
        {
            return (double)freq / 1000000000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 频率KHz转换为Hz
        /// </summary>
        /// <param name="freq">单位:KHz</param>
        /// <returns>单位:Hz</returns>
        public static long FreqKHzToHz(double freq)
        {
            return (long)(freq * 1000);
        }

        /// <summary>
        /// 频率KHz转换为MHz
        /// </summary>
        /// <param name="freq">单位:KHz</param>
        /// <returns>单位:MHz</returns>
        public static double FreqKHzToMHz(long freq)
        {
            return (double)freq / 1000;
        }

        /// <summary>
        /// 频率KHz转换为GHz
        /// </summary>
        /// <param name="freq">单位:KHz</param>
        /// <returns>单位:GHz</returns>
        public static double FreqKHzToGHz(long freq)
        {
            return (double)freq / 1000000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 频率MHz转换为Hz
        /// </summary>
        /// <param name="freq">单位:MHz</param>
        /// <returns>单位:Hz</returns>
        public static long FreqMHzToHz(double freq)
        {
            return (long)(freq * 1000000);
        }

        /// <summary>
        /// 频率MHz转换为KHz
        /// </summary>
        /// <param name="freq">单位:MHz</param>
        /// <returns>单位:KHz</returns>
        public static long FreqMHzToKHz(double freq)
        {
            return (long)(freq * 1000);
        }

        /// <summary>
        /// 频率MHz转换为GHz
        /// </summary>
        /// <param name="freq">单位:MHz</param>
        /// <returns>单位:GHz</returns>
        public static double FreqMHzToGHz(long freq)
        {
            return (double)freq / 1000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 频率GHz转换为MHz
        /// </summary>
        /// <param name="freq">单位:GHz</param>
        /// <returns>单位:MHz</returns>
        public static long FreqGHzToMHz(double freq)
        {
            return (long)(freq * 1000);
        }

        /// <summary>
        /// 频率GHz转换为KHz
        /// </summary>
        /// <param name="freq">单位:GHz</param>
        /// <returns>单位:KHz</returns>
        public static long FreqGHzToKHz(double freq)
        {
            return (long)(freq * 1000000);
        }

        /// <summary>
        /// 频率GHz转换为Hz
        /// </summary>
        /// <param name="freq">单位:GHz</param>
        /// <returns>单位:Hz</returns>
        public static long FreqGHzToHz(double freq)
        {
            return (long)(freq * 1000000000);
        }
        #endregion


        #region 带宽转换

        /// <summary>
        /// 带宽Hz转换为KHz
        /// </summary>
        /// <param name="bw">单位:Hz</param>
        /// <returns>单位:KHz</returns>
        public static double BwHzToKHz(long bw)
        {
            return (double)bw / 1000;
        }

        /// <summary>
        /// 带宽Hz转换为MHz
        /// </summary>
        /// <param name="bw">单位:hz</param>
        /// <returns>单位:MHz</returns>
        public static double BwHzToMHz(long bw)
        {
            return (double)bw / 1000000;
        }

        /// <summary>
        /// 带宽Hz转换为GHz
        /// </summary>
        /// <param name="bw">单位:hz</param>
        /// <returns>单位:GHz</returns>
        public static double BwHzToGHz(long bw)
        {
            return (double)bw / 1000000000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 带宽KHz转换为Hz
        /// </summary>
        /// <param name="bw">单位:KHz</param>
        /// <returns>单位:Hz</returns>
        public static long BwKHzToHz(double bw)
        {
            return (long)(bw * 1000);
        }

        /// <summary>
        /// 带宽KHz转换为MHz
        /// </summary>
        /// <param name="bw">单位:KHz</param>
        /// <returns>单位:MHz</returns>
        public static double BwKHzToMHz(long bw)
        {
            return (double)bw / 1000;
        }

        /// <summary>
        /// 带宽KHz转换为GHz
        /// </summary>
        /// <param name="bw">单位:KHz</param>
        /// <returns>单位:GHz</returns>
        public static double BwKHzToGHz(long bw)
        {
            return (double)bw / 1000000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 带宽MHz转换为Hz
        /// </summary>
        /// <param name="bw">单位:MHz</param>
        /// <returns>单位:Hz</returns>
        public static long BwMHzToHz(double bw)
        {
            return (long)(bw * 1000000);
        }

        /// <summary>
        /// 带宽MHz转换为KHz
        /// </summary>
        /// <param name="bw">单位:MHz</param>
        /// <returns>单位:KHz</returns>
        public static long BwMHzToKHz(double bw)
        {
            return (long)(bw * 1000);
        }

        /// <summary>
        /// 带宽MHz转换为GHz
        /// </summary>
        /// <param name="bw">单位:MHz</param>
        /// <returns>单位:GHz</returns>
        public static double BwMHzToGHz(long bw)
        {
            return (double)bw / 1000;
        }

        //---------------------------------------------------------------

        /// <summary>
        /// 带宽GHz转换为MHz
        /// </summary>
        /// <param name="bw">单位:GHz</param>
        /// <returns>单位:MHz</returns>
        public static long BwGHzToMHz(double bw)
        {
            return (long)(bw * 1000);
        }

        /// <summary>
        /// 带宽GHz转换为KHz
        /// </summary>
        /// <param name="bw">单位:GHz</param>
        /// <returns>单位:KHz</returns>
        public static long BwGHzToKHz(double bw)
        {
            return (long)(bw * 1000000);
        }

        /// <summary>
        /// 带宽GHz转换为Hz
        /// </summary>
        /// <param name="bw">单位:GHz</param>
        /// <returns>单位:Hz</returns>
        public static long BwGHzToHz(double bw)
        {
            return (long)(bw * 1000000000);
        }
        #endregion



        #region 计算机容量转换
        /// <summary>
        /// KB字节数
        /// </summary>
        public const long KB_BYTES = 1024L;

        /// <summary>
        /// MB字节数
        /// </summary>
        public const long MB_BYTES = 1048576L;

        /// <summary>
        /// GB字节数
        /// </summary>
        public const long GB_BYTES = 1073741824L;

        /// <summary>
        /// TB字节数
        /// </summary>
        public const long TB_BYTES = 1099511627776L;

        /// <summary>
        /// PB字节数
        /// </summary>
        public const long PB_BYTES = 1125899906842624L;


        /// <summary>
        /// 字节数转换为KB数
        /// </summary>
        /// <param name="value">字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>KB字节数</returns>
        public static double ByteToKB(long value, int digits = 1)
        {
            return Math.Round((double)value / KB_BYTES, digits);
        }

        /// <summary>
        /// 字节数转换为MB数
        /// </summary>
        /// <param name="value">字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>MB数</returns>
        public static double ByteToMB(long value, int digits = 1)
        {
            return Math.Round((double)value / MB_BYTES, digits);
        }

        /// <summary>
        /// 字节数转换为GB数
        /// </summary>
        /// <param name="value">字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>GB字节数</returns>
        public static double ByteToGB(long value, int digits = 1)
        {
            return Math.Round(((double)value / GB_BYTES), digits);
        }

        /// <summary>
        /// 字节数转换为TB数
        /// </summary>
        /// <param name="value">字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>TB字节数</returns>
        public static double ByteToTB(long value, int digits = 1)
        {
            return Math.Round(((double)value / TB_BYTES), digits);
        }

        /// <summary>
        /// 字节数转换为PB字节数
        /// </summary>
        /// <param name="value">字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>PB字节数</returns>
        public static double ByteToPB(long value, int digits = 1)
        {
            return Math.Round(((double)value / PB_BYTES), 1);
        }

        //---------------------------------------------------------------

        /// <summary>
        /// KB字节数转换为PB字节数
        /// </summary>
        /// <param name="value">KB字节数</param>
        /// <returns>字节数</returns>
        public static long KByteToByte(double value)
        {
            return (long)(value * KB_BYTES);
        }

        /// <summary>
        /// KB字节数转换为PB字节数
        /// </summary>
        /// <param name="value">KB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>MB字节数</returns>
        public static double KByteToMB(long value, int digits = 1)
        {
            return Math.Round((double)value / KB_BYTES, digits);
        }

        /// <summary>
        /// KB字节数转换为PB字节数
        /// </summary>
        /// <param name="value">KB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>GB字节数</returns>
        public static double KByteToGB(long value, int digits = 1)
        {
            return Math.Round(((double)value / MB_BYTES), digits);
        }

        /// <summary>
        /// KB字节数转换为PB字节数
        /// </summary>
        /// <param name="value">KB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>TB字节数</returns>
        public static double KByteToTB(long value, int digits = 1)
        {
            return Math.Round(((double)value / GB_BYTES), digits);
        }

        /// <summary>
        /// KB字节数转换为PB字节数
        /// </summary>
        /// <param name="value">KB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>PB字节数</returns>
        public static double KByteToPB(long value, int digits = 1)
        {
            return Math.Round(((double)value / TB_BYTES), 1);
        }

        //---------------------------------------------------------------

        /// <summary>
        /// MB字节数转换为字节数
        /// </summary>
        /// <param name="value">MB字节数</param>
        /// <returns>字节数</returns>
        public static long MByteToByte(double value)
        {
            return (long)(value * MB_BYTES);
        }

        /// <summary>
        /// MB字节数转换为PB数
        /// </summary>
        /// <param name="value">MB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>KB字节数</returns>
        public static double MByteToKB(double value, int digits = 1)
        {
            return Math.Round(value * KB_BYTES, digits);
        }

        /// <summary>
        /// MB字节数转换为PB数
        /// </summary>
        /// <param name="value">MB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>GB字节数</returns>
        public static double MByteToGB(long value, int digits = 1)
        {
            return Math.Round(((double)value / KB_BYTES), digits);
        }

        /// <summary>
        /// MB字节数转换为PB数
        /// </summary>
        /// <param name="value">MB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>TB字节数</returns>
        public static double MByteToTB(long value, int digits = 1)
        {
            return Math.Round(((double)value / MB_BYTES), digits);
        }

        /// <summary>
        /// MB字节数转换为PB数
        /// </summary>
        /// <param name="value">MB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>PB字节数</returns>
        public static double MByteToPB(long value, int digits = 1)
        {
            return Math.Round(((double)value / GB_BYTES), 1);
        }

        //---------------------------------------------------------------

        /// <summary>
        /// GB字节数转换为字节数
        /// </summary>
        /// <param name="value">GB字节数</param>
        /// <returns>字节数</returns>
        public static long GByteToByte(double value)
        {
            return (long)(value * GB_BYTES);
        }

        /// <summary>
        /// GB字节数转换为KB数
        /// </summary>
        /// <param name="value">GB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>KB字节数</returns>
        public static double GByteToKB(double value, int digits = 1)
        {
            return Math.Round(value * MB_BYTES, digits);
        }

        /// <summary>
        /// GB字节数转换为mB数
        /// </summary>
        /// <param name="value">GB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>MB字节数</returns>
        public static double GByteToMB(double value, int digits = 1)
        {
            return Math.Round(value * KB_BYTES, digits);
        }

        /// <summary>
        /// GB字节数转换为TB数
        /// </summary>
        /// <param name="value">GB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>TB字节数</returns>
        public static double GByteToTB(long value, int digits = 1)
        {
            return Math.Round(((double)value / KB_BYTES), digits);
        }

        /// <summary>
        /// GB字节数转换为PB数
        /// </summary>
        /// <param name="value">GB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>PB字节数</returns>
        public static double GByteToPB(long value, int digits = 1)
        {
            return Math.Round(((double)value / MB_BYTES), 1);
        }

        //---------------------------------------------------------------

        /// <summary>
        /// TB字节数转换为字节数
        /// </summary>
        /// <param name="value">TB字节数</param>
        /// <returns>字节数</returns>
        public static long TByteToByte(double value)
        {
            return (long)(value * TB_BYTES);
        }

        /// <summary>
        /// TB字节数转换为KB数
        /// </summary>
        /// <param name="value">TB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>KB字节数</returns>
        public static double TByteToKB(double value, int digits = 1)
        {
            return Math.Round(value * GB_BYTES, digits);
        }

        /// <summary>
        /// TB字节数转换为MB数
        /// </summary>
        /// <param name="value">TB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>MB字节数</returns>
        public static double TByteToMB(double value, int digits = 1)
        {
            return Math.Round(value * MB_BYTES, digits);
        }

        /// <summary>
        /// TB字节数转换为GB数
        /// </summary>
        /// <param name="value">TB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>GB字节数</returns>
        public static double TByteToGB(double value, int digits = 1)
        {
            return Math.Round(value * KB_BYTES, digits);
        }

        /// <summary>
        /// TB字节数转换为PB数
        /// </summary>
        /// <param name="value">TB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>PB字节数</returns>
        public static double TByteToPB(long value, int digits = 1)
        {
            return Math.Round(((double)value / KB_BYTES), 1);
        }

        //---------------------------------------------------------------

        /// <summary>
        /// PB字节数转换为字节数
        /// </summary>
        /// <param name="value">PB字节数</param>
        /// <returns>字节数</returns>
        public static long PByteToByte(double value)
        {
            return (long)(value * PB_BYTES);
        }

        /// <summary>
        /// PB字节数转换为KB数
        /// </summary>
        /// <param name="value">PB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>KB字节数</returns>
        public static double PByteToKB(double value, int digits = 1)
        {
            return Math.Round(value * TB_BYTES, digits);
        }

        /// <summary>
        /// PB字节数转换为MB数
        /// </summary>
        /// <param name="value">PB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>MB字节数</returns>
        public static double PByteToMB(double value, int digits = 1)
        {
            return Math.Round(value * GB_BYTES, digits);
        }

        /// <summary>
        /// PB字节数转换为GB数
        /// </summary>
        /// <param name="value">PB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>GB字节数</returns>
        public static double PByteToGB(double value, int digits = 1)
        {
            return Math.Round(value * MB_BYTES, digits);
        }

        /// <summary>
        /// PB字节数转换为TB数
        /// </summary>
        /// <param name="value">PB字节数</param>
        /// <param name="digits">保留小数位数,默认1位</param>
        /// <returns>TB字节数</returns>
        public static double PByteToTB(double value, int digits = 1)
        {
            return Math.Round(value * KB_BYTES, digits);
        }
        #endregion


    }
}
